//
//  OrderDAO.swift
//  DiDiRecycle
//
//  Created by abb on 16/7/26.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit

class OrderDAO: NSObject {
    
    class func getAroundOrder(center:String,finished:(result:[[String:AnyObject]]?,error:String?)->()) {
        let url = "http://yuntuapi.amap.com/datasearch/around"
        
        let params = [
            "key" : "07577911646537ec9b006d56c6880289",
            "tableid" : "57ac8a8eafdf520b8952c106",
            "center" : center,
        ]
        NetworkTools.shareNetwortTools().request(.GET, url, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.value != nil {
                if response.result.value!["status"] as! Int != 1 {
                    let error = response.result.value!["info"] as! String
                    finished(result: nil, error: error)
                    return
                }
                let result = response.result.value!["datas"] as! [[String:AnyObject]]
                finished(result: result, error: nil)
            } else {
                finished(result: nil, error: "网络错误")
            }
        }
    }
    class func orderReceiving(orderId:Int,DGYunTuId:Int,finished:(success:Bool,result:String?)->()) {
        let path = kUrl + "orders/orderReceiving"
        
        let params = [
            "merchant_id" : User.loadUser()!.id!,
            "id" : orderId,
            "DGYunTu_id" : DGYunTuId
        ]
        
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.error == nil {
                let result = response.result.value!["result"] as! String
                if response.result.value!["code"] as! String == "1" {
                    finished(success: true, result: result)
                } else {
                    finished(success: false, result: result)
                }
            } else {
                finished(success: false, result: "网络错误")
            }
        }
        
    }
    
    class func getOrderList(userID:Int,finished:(result:[[String:AnyObject]]?,error:String?)->()) {
        
        
        let path = "http://yuntuapi.amap.com/datamanage/data/list"
        
        let params = [
            "key" : "07577911646537ec9b006d56c6880289",
            "tableid" : "57ac8a8eafdf520b8952c106",
            "filter":"merchant_id:\(userID)"
            ]
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            // 判断网络错误
            if response.result.error == nil {
                if response.result.value!["status"] as! Int != 1 {
                    let error = response.result.value!["info"] as! String
                    finished(result: nil, error: error)
                    return
                }
                let result = response.result.value!["datas"] as! [[String:AnyObject]]
                finished(result: result, error: nil)
            } else {
                // 打印网络错误
                print(response.result.error)
                finished(result: nil,error:"网络错误")
            }
        }
        
    }

}
